<?php

class Manga extends Member_Controller {

    public function __construct() {
        parent::__construct();

        //Set folder to load view (under View folder)
        $this->layouts->set_view_folder('manga');

        //$this->layouts->set_layout('home');
        //Load models
        $this->load->model('manga_model');
        $this->load->model('admin/manga_manage_model');

        //Load libraries
        //$this->output->enable_profiler(TRUE);
    }

    function index() {

        if ($this->session->userdata('manga_id') !== FALSE) {
            $this->session->unset_userdata('manga_id');
        }

        $this->display_mangas();
    }

    function display_mangas() {

        //Load neccessary JS and CSS
        $this->layouts->set_css('tooltip/style');

        //$this->user_id is from session up paren class Member_Controller


        $reading_mangas = $this->manga_model->get_reading_mangas($this->user_id);

        switch ($reading_mangas) {
            case '': //First time access, no reading manga -> pick somes for user
                $data['mangas_list'] = $this->manga_model->get_mangas_list($this->user_id);
                $this->layouts->view('mangas_list', $data);
                break;

            case 'done': //Finished all the magnas -> show test results
                $data['test_result'] = $this->manga_model->get_test_result($this->user_id);
                $this->layouts->view('test_result', $data);
                break;

            default: //Read halfway -> show reading list
                $data['mangas_list'] = $this->manga_model->get_mangas_list($this->user_id, $reading_mangas);
                $this->layouts->view('mangas_list', $data);
        }
    }

    function read_manga() {
        //Load neccessary JS and CSS
        $this->layouts->set_css('jqueryUI/jquery-ui-1.8.16.custom');
        $this->layouts->set_css('manga/style');

        //1st time: There is no page at segment 4 of URL and manga_id is at segment 3 --> store it in session
        //Store manga_id into session (to be deleted after user finished reading one manga)
        if ($this->uri->segment(3) !== FALSE) {
            if ($this->session->userdata('manga_id') === FALSE) {
                $manga_id = $this->uri->segment(3);
                $this->session->set_userdata('manga_id', $manga_id);
            } else {
                $manga_id = $this->session->userdata('manga_id');
            }

            $all_characters = "";

            $current_page = $this->uri->segment(4);

            $total_page = $this->manga_manage_model->get_last_page($manga_id); //total_page = last page also

            switch ($current_page) {
                case $current_page == 1 && $current_page != $total_page:
                    $next_page = $current_page + 1;
                    $prev_page = "#";
                    break;

                case $current_page == 1 && $current_page == $total_page:
                    $next_page = "#";
                    $prev_page = "#";

                    $all_characters = $this->manga_manage_model->get_all_characters($manga_id);
                    break;

                case $total_page:
                    $prev_page = $current_page - 1;
                    $next_page = "#";

                    $all_characters = $this->manga_manage_model->get_all_characters($manga_id);
                    break;

                default:
                    $next_page = $current_page + 1;
                    $prev_page = $current_page - 1;
            }

            //Prepare the images and show to a new view name manga_view
            $page_content = $this->manga_model->get_page($manga_id, $current_page);


            $data = array(
                "current_page" => $current_page,
                "total_page" => $total_page,
                "next_page" => $next_page,
                "prev_page" => $prev_page,
                "all_characters" => $all_characters,
                "page_content" => $page_content,
                "manga_id" => $manga_id
            );

            $this->layouts->view('read_manga', $data);
        } else {
            redirect('manga');
        }
    }

    function choose_character($character_id) {

        //$this->user_id is from session up paren class Member_Controller
        $manga_id = $manga_id = $this->session->userdata('manga_id');

        //Update score
        $this->manga_model->update_score($this->user_id, $character_id);

        //Remove this manga from user's reading list
        $this->manga_model->remove_manga($this->user_id, $manga_id);

        redirect('manga');
    }

}

/* End of file manga.php */
/* Location: ./application/controllers/manga.php */

